package com.cy.store.controller;

import com.cy.store.controller.ex.*;
import com.cy.store.entity.Address;
import com.cy.store.entity.User;
import com.cy.store.service.IAddressService;
import com.cy.store.service.IUserService;
import com.cy.store.util.JsonResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 地址控制类
 */
@RestController //表示该控制类都以Json格式进行响应
//@Controller
@RequestMapping("address")
public class AddressController extends BaseController {
    @Autowired
    private IAddressService addressService;

    @RequestMapping("add_new_address")
    public JsonResults<Void> addNewAddress(HttpSession httpSession, Address address){
        Integer uid = getuidFromSession(httpSession);
        String username = getUsernameFromSession(httpSession);
        addressService.addNewAddress(uid,username,address);
        return new JsonResults<>(OK);
    }

    @RequestMapping({"","/"})
    public JsonResults<List<Address>> getInfoByUid(HttpSession httpSession){
        List<Address> data = addressService.findInfoByUid(getuidFromSession(httpSession));
        return new JsonResults<>(OK,data);
    }
    //将客户端传入的值放进请求路径中用于获取该值
    @RequestMapping("{aid}/set_default")
    public JsonResults<Void> setDefault(@PathVariable("aid") Integer aid,
                                        HttpSession httpSession){
        Integer uid = getuidFromSession(httpSession);
        String username = getUsernameFromSession(httpSession);
        addressService.updateIsDefault(uid,aid,username);
        return new JsonResults<>(OK);
    }

    @RequestMapping("{aid}/delete")
    public JsonResults<Void> deleteAddress(@PathVariable("aid") Integer aid,
                                           HttpSession httpSession){
        Integer uid = getuidFromSession(httpSession);
        String username = getUsernameFromSession(httpSession);
        addressService.deleteAddressByAid(aid,uid,username);
        return new JsonResults<>(OK);
    }

}
